<?php
    include ("../lib/BLL/DmTrinhDoBLL.php");
    $tb="";
    $tt="";
    $ma="";
    $ten="";
    $tenvt="";
    $btnName="Thêm";
    $trinhdo_bll=new DmTrinhDoBLL();
    $ds_trinhdo=array();
        
    if (isset($_POST['timkiem'])) {
        $keysearch="";
		$where="";
        if(isset($_POST['txtkey'])){
            $keysearch=$_POST['txtkey'];
            $where="WHERE".$trinhdo_bll->buildSearchQueryLike($keysearch);
        }
    }
    
    if(!isset($_POST['timkiem'])||($_POST['txtkey']=="")){
        $where="WHERE 1=1";
    }
    if(isset($_GET['ma'])){
        $btnName="Sửa";
        $madm_td=$_GET['ma'];
        $ds_td=$trinhdo_bll->getItemTrinhdo($madm_td);
        $ma=$ds_td['madm_td'];
        $ten=$ds_td['ten_td'];
        $tenvt=$ds_td['ten_vt_td'];
        $tt="readonly";
    }
    if(isset($_POST['btnLuu'])){
        $trd=new DmTrinhDo();
        $madm_td=$_POST['madm_td'];
        $ten_td=$_POST['ten_td'];
        $ten_vt_td=$_POST['ten_vt_td'];
        if($btnName=="Thêm"){
            $trd->setDmTrinhDo($madm_td,$ten_td,$ten_vt_td);
            $kq=$trinhdo_bll->InsertTrinhDo($trd);
            if($kq==true){
                header("location: chuongtrinhdaotao-trinhdo");
            }
        }else{
            $trd->setDmTrinhDo($madm_td,$ten_td,$ten_vt_td);
            $kq=$trinhdo_bll->UpdateTrinhDo($trd);
            if ($kq == true){
                header("location: chuongtrinhdaotao-trinhdo");
            }
        }
    }
    if(isset($_GET['xoama'])){
        $maxoa=$_GET['xoama'];
        $kq=$trinhdo_bll->DelTrinhDo($maxoa);
        if ($kq == true){
            header("location: chuongtrinhdaotao-trinhdo");
        }
    }
    if(isset($_POST['xoachon'])){
        if(isset($_POST['checkall'])){
            $kq=$trinhdo_bll->deleteAll();
            if ($kq == true){
                header("location: chuongtrinhdaotao-trinhdo");
            }
        }
        if(isset($_POST['checkbox'])){
            $delete=$_POST['checkbox'];
            foreach($delete as $madm_td){
                $kq=$trinhdo_bll->DelTrinhDo($madm_td);
                if ($kq == true){
                    header("location: chuongtrinhdaotao-trinhdo");
                }
            }
        }
    }

    $ds_trinhdo=$trinhdo_bll->timkiem($where);
    //$ds_trinhdo=$trinhdo_bll->GetlistTrinhDo();
    $totalRecords=count($ds_trinhdo);        
    $p=(isset($_GET['p']))?$_GET['p']:1;
    $limit=35;                       
    $offset=($p-1)*$limit;
    $ds_trinhdo=$trinhdo_bll->getPages($where,$offset,$limit);
    $url="chuongtrinhdaotao-trinhdo";
    $pages=$trinhdo_bll->paging($totalRecords,$limit,$url,'pagers');

    $smarty->assign('p',$p);
    $smarty->assign('pages',$pages);
    $smarty->assign('ma',$ma);
    $smarty->assign('ten',$ten);
    $smarty->assign('tenvt',$tenvt);
    $smarty->assign('btnName',$btnName);
    $smarty->assign('tt',$tt);
    $smarty->assign('ds_trinhdo',$ds_trinhdo);
    $smarty->display('chuongtrinhdaotao/dmTrinhdo.tpl');
?>
